/*
 * Copyright 2011-2016 YueJi.com All right reserved. This software is the
 * confidential and proprietary information of YueJi.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with YueJi.com.
 */
package com.bops.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.bops.biz.permission.Menu;
import com.bops.biz.permission.UserGrant;
import com.bops.service.CommonServiceLocator;
import com.bops.valve.webuser.BopsWebUser;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;
 
/**
 * 类BospWidgetController.java的实现描述：TODO 类实现描述 
 */
public class BopsWidgetController extends AbstractController{
    private static Date startDate;

  
    
    @ControllerAction
    public WebResult header(Map<String, Object> model) throws Exception {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        if (bopsWebUser!=null)
        {
            model.put("userName",bopsWebUser.getName());
        }
        return new View("/common/header.vm");
    }
    
    @ControllerAction
    public WebResult footer(Map<String, Object> model) throws Exception {
        if (startDate==null)
        {
            startDate = new Date();
        }
        model.put("startDate",startDate);
        return new View("/common/footer.vm");
    }
    
    
    @ControllerAction
    public WebResult systemMenu(Map<String, Object> model,HttpServletRequest request) throws Exception {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        if (bopsWebUser!=null)
        {
            UserGrant userGrant = bopsWebUser.getUserGrant();
            if (userGrant!=null)
            {
                List<Menu> allMenuList = CommonServiceLocator.getAllMenuList();
                List<MenuBarVO> menuBarVOList = new ArrayList<MenuBarVO>();
                for (Menu menu:allMenuList)
                {
                    if (menu != null && menu.getList() != null) {
                        MenuBarVO menuBarVO = null;
                        List<String> tmp = menu.getList();
                        int size = tmp.size();
                        for (int i = 0; i < size; i = i + 2) {
                            String tmpMenuIetmName =  tmp.get(i + 1);
                            if (userGrant.checkMenu(menu.getId(),tmpMenuIetmName))
                            { 
                                if (menuBarVO==null) 
                                {
                                    menuBarVO = new MenuBarVO(menu.getTitle(),menu.getId()); 
                                }
                                menuBarVO.addMenuUnit(new MenuUnitVO(tmp.get(i),tmpMenuIetmName));
                            }
                        }
                        if (menuBarVO!=null)
                        {
                        	
                            menuBarVOList.add(menuBarVO);
                        } 
                    }
                }
                
                String[] showIDs = getShowMenuBar(request);
                if (showIDs!=null)
                {
                    for (String showID:showIDs)
                    {
                        for (MenuBarVO bar:menuBarVOList)
                        {
                            if (bar.getId().equalsIgnoreCase(showID))
                            {
                                bar.setShow("true");
                            }
                        }
                    } 
                }
                model.put("allMenuBarList",menuBarVOList);
            }
        }
        return new View("/common/systemMenu.vm");
    }
        
    /**
     * 从Cookie中取出需要显示的菜单条
     * @param request
     * @return
     */
    private String[] getShowMenuBar(HttpServletRequest request)
    {
        Cookie   cookies[]=request.getCookies(); 
        Cookie   sCookie=null; 
        String   svalue=null; 
        String   sname=null; 
        if (cookies!=null)
        {
            for(int   i=0;i <cookies.length;i++) 
            { 
                sCookie=cookies[i]; 
                sname=sCookie.getName(); 
                if ("menuConfig".equals(sname))
                {
                    svalue=sCookie.getValue(); 
                    if (svalue!=null)
                    {
                        return svalue.split("-_-");
                    }
                    return null;
                }
            }
        }
        return null;
    }
    
    public static class MenuBarVO {

        public MenuBarVO(String title,String id) {
           this.title = title;
           this.id = id;
        }
        public void addMenuUnit(MenuUnitVO vo)
        {
            if (menuUnitList==null)
            {
                menuUnitList = new ArrayList<MenuUnitVO>();
            }
            menuUnitList.add(vo);
        }
        
        public String getTitle() {
            return title;
        }
        
        public List<MenuUnitVO> getMenuUnitList() {
            return menuUnitList;
        }
        public String getId() {
            return id;
        }
        public String getShow() {
            return show;
        }
        public void setShow(String show) {
            this.show = show;
        }
        private String show;
        private String id;
        private String title;
        private List<MenuUnitVO> menuUnitList;
    }
    
    public static class MenuUnitVO {

        public MenuUnitVO(String url, String name) {
            this.url = url;
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public String getName() {
            return name;
        }
        
        private String url;
        private String name;
    }
}
